/*
********************************************************************************
*
*	file : mian.c
*
********************************************************************************
*/
#include "sys_task.h" 
#include "pincfg.h"
#include "bsp_hal.h"
#include "sys_com.h"
#include "dataproc.h"
#include "mytype.h"
#include "swap.h"
#include "mbcom.h"
#include "usb_file.h" 
#include "flash_task.h" 
#include "simp_txt_analysis.h" 


/*
********************************************************************************
*                               fun
********************************************************************************
*/
extern const txt_cmd_cfg_type cmd_list[];

const char file_cmd_name[] = "0:YS/CMD.TXT";

/*******************************************************************************
* @brief  usb_file_cmd_list_handle.
* \param[in] none
* \retval: none
*******************************************************************************/
void usb_file_cmd_list_handle(void)
{
    int index = 0, cnt;
    char b_info_out = 0;
    
    if (f_open(&usb_opt.file, file_cmd_name, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)
    {
        return ;
    } 
    
    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"-----------------------------------------------------------\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"------------------------ CMD  LIST ------------------------\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"-----------------------------------------------------------\r\n");
    
	// 保存
	f_write(&usb_opt.file, &usb_opt.file_buff[0], index, &usb_opt.file_read_len_back); 
	index = 0;
	
    for(cnt = 0;cmd_list[cnt].format != TXT_CMD_TYPE_END; cnt++)
    {
        int amount;
        
        //跳过工厂设置命令
        if(cmd_list[cnt].attr & 0x4000)
            continue;
        
        if(((cmd_list[cnt].attr >> 8) & 0x000F) >= 2 )
            continue;
        
        if(((cmd_list[cnt].attr >> 8) & 0x000F) > 0 )
            index += sprintf((char *)&usb_opt.file_buff[index],"%-10s  <ADMIN>    ", cmd_list[cnt].cmd);
        else
            index += sprintf((char *)&usb_opt.file_buff[index],"%-10s  <ANYONE>   ", cmd_list[cnt].cmd);
        
        if(!b_info_out && (0 == strcmp("OUT", cmd_list[cnt].cmd)))
        {
            index += sprintf((char *)&usb_opt.file_buff[index],"sample :: OUT = TXT or OUT = MDT \r\n");
            b_info_out = 1;
            continue;
        }
        
        switch(cmd_list[cnt].format)
        {
            case TXT_CMD_TYPE_CHAR:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s = xxx", cmd_list[cnt].cmd);
                for(amount=1; amount < cmd_list[cnt].len_max; amount++)
                    index += sprintf((char *)&usb_opt.file_buff[index],", xxx");
                    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n");
                break;
            }
            case TXT_CMD_TYPE_SHORT:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s = xxx", cmd_list[cnt].cmd);
                for(amount=1; amount < cmd_list[cnt].len_max; amount++)
                    index += sprintf((char *)&usb_opt.file_buff[index],", xxx");
                    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n");
                break;
            }
            case TXT_CMD_TYPE_LONG:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s = xxx", cmd_list[cnt].cmd);
                for(amount=1; amount < cmd_list[cnt].len_max; amount++)
                    index += sprintf((char *)&usb_opt.file_buff[index],", xxx");
                    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n");
                break;
            }
            case TXT_CMD_TYPE_FLOAT:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s = xxx.x", cmd_list[cnt].cmd);
                for(amount=1; amount < cmd_list[cnt].len_max; amount++)
                    index += sprintf((char *)&usb_opt.file_buff[index],", xxx.x");
                    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n");
                break;
            }
            case TXT_CMD_TYPE_STRING:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s = string \r\n", cmd_list[cnt].cmd);
                break;
            }
            case TXT_CMD_TYPE_NUMBER:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: xxx \r\n");
                break;
            }
            case TXT_CMD_TYPE_NOP:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s\r\n", cmd_list[cnt].cmd);
                break;
            }
            default:
            {
                break;
            }
        }
		
		if(index >= FILE_BUFF_SIZE/2)
		{
			// 保存
			f_write(&usb_opt.file, &usb_opt.file_buff[0], index, &usb_opt.file_read_len_back); 
			index = 0;
		}
    }
    
    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"xxx is a number !\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"-----------------------------------------------------------\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"------------------------    END    ------------------------\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"-----------------------------------------------------------\r\n");
    
    f_write(&usb_opt.file, &usb_opt.file_buff[0], index, &usb_opt.file_read_len_back); 
    
    
    f_close(&usb_opt.file);
}

